<?php

namespace App\Http\Controllers\Plugin;

use AlibabaCloud\Client\AlibabaCloud;
use AlibabaCloud\Client\Exception\ClientException;
use AlibabaCloud\Client\Exception\ServerException;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Str;
use Modules\Member\Entities\ZxPackageMember;
use Modules\Member\Entities\ZxShopPoster;
use Intervention\Image\ImageManagerStatic as Image;
use SimpleSoftwareIO\QrCode\Facades\QrCode;

trait PosterController
{
    use OssController;
    //获取图片名称
    public function name($url){
        $arr = explode('.',basename($url));
        return array_shift($arr);
    }
    //图片裁圆
    public function imageCeil($url , $width = 109 , $height = 109)
    {
        $file_path = public_path('storage/images/avatar');
        $file = $file_path.'/'.$this->name($url).'-ceil.png';
//        if(file_exists($file)){
//            return $file;
//        }
        Storage::makeDirectory('public/images/avatar');//创建存储文件
        $img = Image::make($url)->resize($width , $height);
        $new = Image::canvas($width , $height);
        $r = $img->width() /2;
        for($x=0;$x<$img->width();$x++) {
            for($y=0;$y<$img->height();$y++) {
                $c=$img->pickColor($x,$y,'array');
                if(((($x-$r) * ($x-$r) + ($y-$r) * ($y-$r)) < ($r*$r))) {
                    $new->pixel($c,$x,$y);
                }
            }
        }
        $new->save($file);
        return $file;
    }
    //获取当前用户信息
    protected function getMember($id){
        return ZxPackageMember::find($id);
    }
    protected function getPoster($id){
        return ZxShopPoster::where($id,$id)->value('image');
    }
    //生成参数二维码
    protected function qrcode($code){
        $fileName = 'storage/images/ercode/'.Str::random('6').'.png';
        Storage::makeDirectory('public/images/ercode');//创建存储文件
        QrCode::format('png')->margin(0)->size(115)->generate(env('APP_URL_SITE','').'/dszj.html#/mobile/register?invitation_code='.$code,$fileName);
        return $fileName;
    }
    //工厂制作
    public function factory($request,$fileName){
        $img = Image::make($request->background_image)->resize(720, 1200);//public/images/ddk/base.png
        //插入头像 x-oss-process=image/resize,m_fill,h_100,w_100
        $avatar = $this->imageCeil($request->user()->avatar);
        //$avatar = $request->user()->avatar.'?x-oss-process=image/circle,r_54/format,png';
        $img->insert($avatar , 'bottom-left' , 54 , 75);
        //插入二维码
        $qrcode = $this->qrcode($request->user()->invitation_code);
        $img->insert($qrcode , 'top-right' , 47 , 60);
        //添加昵称
//        $img->text(Str::limit($request->user()->username,10,'***'), 70, 1120, function($font) {
//            $font->file('fonts/msyh.ttf');
//            $font->size(18);
//            $font->color('#000');
//            $font->angle(0);
//        });
        //添加会员等级
//        $img->text($request->level, 87, 1155, function($font) {
//            $font->file('fonts/msyh.ttf');
//            $font->size(18);
//            $font->color('#000');
//            $font->angle(0);
//        });
        $img->save($fileName);
        return $fileName;
    }
    //更新海报
    //?x-oss-process=image/circle,r_100/format,png
    public function updatePoster($request){
        $fileName = 'storage/images/poster/share_image/'.'我的推广海报-'.$request->user()->id.$request->category.$request->number.'.jpg';//options
        Storage::delete(str_replace('storage/','public/',$fileName));
       $this->factory($request,$fileName);
        return $fileName.'?'.time();
    }
    //生成海报
    public function makePoster($request)
    {
        $fileName = 'storage/images/poster/share_image/'.'我的推广海报-'.$request->user()->id.$request->category.$request->number.'.jpg';//options
        if(file_exists($fileName)){
            return $fileName.'?'.time();
        }
        Storage::makeDirectory('public/images/poster/share_image');//创建存储文件
        $this->factory($request,$fileName);
        return $fileName.'?'.time();
    }

}
